import 'dart:io';
import 'package:full_display/unit/navigation/navigation_util.dart';
import 'package:full_display/unit/screen/screen_utility.dart';
import 'package:flutter/material.dart';
import 'package:full_display/routers/route_name.dart';
import 'package:full_display/unit/socket/http/http_server_utils.dart';
import 'package:full_display/unit/socket/udp/socket_udp_utils.dart';
import 'package:full_display/unit/web_socket/web_sockect_utils.dart';
import 'index_page.dart';

class SplashPage extends StatefulWidget {
  static const String sName = "/splash";
  const SplashPage({super.key});
  @override
  State<SplashPage> createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {
  late double width;
  late double height;
  bool hasInitialized = false;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance?.addPostFrameCallback((timeStamp) async {
      await _init();
    });
  }

  Future<void> _init() async {
    if (hasInitialized) return;

    hasInitialized = true;
    Future.delayed(Duration(seconds: 2), () {
      NavigationUtil.getInstance().pushReplacementPage(
          context, RouteNames.indexPage,
          widget: const IndexPage());
      // widget: const LoginWidget());
    });
  }

  @override
  void dispose() {
    hasInitialized = false;
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    WebSocketUtils().initData(context);
    WebSocketUtils().initSocket();
    // SocketUDPUtils().initData(context);
    // SocketUDPUtils().initALlUDP();
    // HttpServerUtils().initHttpServer();
    width = MediaQuery.of(context).size.width;
    height = MediaQuery.of(context).size.height;
    var aspectRatio = MediaQuery.of(context).size.aspectRatio;
    var hwAspectRatio = 1 / aspectRatio;

    debugPrint(
        'object=aspectRatio=${MediaQuery.of(context).size.aspectRatio}=hwAspectRatio=$hwAspectRatio，width=$width,height=$height');
    if (aspectRatio > 0) {
      if (hwAspectRatio > 1.87 && Platform.isAndroid) {
        ScreenUtilityCustomInit(context, 414, 896);
      } else {
        ScreenUtilityStandardInit(context); //初始化ScreenUtility
      }
    }
    return Container(
      width: MediaQuery.of(context).size.width,
      height: MediaQuery.of(context).size.height,
      child: Image.asset(
        'assets/images/splash.png',
        fit: BoxFit.cover,
      ),
    );
  }
}
